/**
 * 
 */
package cn.biplam.front.component.view.mdManage.buffer2md.main;

import cn.biplam.common.client.DataFilter;
import cn.biplam.common.client.Operator;
import cn.biplam.common.client.OperatorResult;
import cn.biplam.common.client.Paging;
import cn.biplam.common.opcode.sqlengine.DDLParserCode.BufCompTabCode;
import cn.biplam.common.opcode.sqlengine.DDLParserCode.BufCompTabColCode;
import cn.biplam.common.struc.MdStruc;
import cn.biplam.common.tools.TOOLS;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.UserInfo;
import cn.biplam.front.component.AjxResponse;
import cn.biplam.front.component.data.common.CaptionComp;
import cn.biplam.front.component.view.mdManage.buffer2md.main.callback.Buffer2MdColOperatorCallBack;
import cn.biplam.front.component.view.mdManage.buffer2md.main.callback.Buffer2MdDealCallBack;
import cn.biplam.front.html.HtmlDiv;
import cn.biplam.front.html.base.HtmlWindow;
import cn.biplam.front.html.base.Const.Length_Unit;
import cn.biplam.front.html.base.Const.Window_Event;
import cn.biplam.front.template.control.FlatTable;
import cn.biplam.front.template.control.FlowTable;
import cn.biplam.front.template.control.table.TablePropertys;
import cn.biplam.front.template.pageFrame.mdManage.MdObject;
import cn.biplam.front.template.pageFrame.select.ContentPage;

/**
 * 	buffer2md表列比较管理页面
 * @author 	WangSong 2009-5-15 下午12:02:43
 * @version 1.0
 */
public class Buffer2MdCompareTableColManage extends AjxResponse{

	public  final static String cId="2204";
	public final static String aId="220401";
	private String width="680";
	private String showPageLine="12";

	@Override
	protected HtmlWindow ajxGet() throws ErrorMessage {
		return get(recordId,mUser,sHomeUrl);
	}
	
	/**
	 * 获取数据仓库模型表详细信息管理界面
	 * @param recordId 数据仓库表记录ID
	 * @param mUser    用户
	 * @throws ErrorMessage
	 */
	public HtmlWindow get(String recordId,UserInfo mUser,String sHomeUrl) throws ErrorMessage{
		if(this.recordId==null){
			this.recordId = recordId;
		}
		if(this.mUser==null){
			this.mUser = mUser;
		}
		if(this.sHomeUrl==null){
			this.sHomeUrl = sHomeUrl;
		}
		MdObject mmp=new MdObject(cId,recordId);
		ContentPage cp = new ContentPage(cId);
		/** 放入Select的说明信息 */
		cp.put(getManage());
		
		mmp.putTable(cp.get());
		/** 放入操作按钮组件 */
		mmp.putOperator(new Buffer2MdTableColButton().get(recordId, mUser));
		return mmp.get();
	}
	
	private HtmlWindow getManage() throws ErrorMessage{
		HtmlWindow manage = new HtmlDiv();
		/** 表描述信息*/
		getTableDesc(manage);
		/** 表列信息*/
		getTableCol(manage);
		return manage;
	}
	
	private void getTableDesc(HtmlWindow manage) throws ErrorMessage{
		DataFilter filter=new DataFilter();
		filter.put(MdStruc.ImpCompTabStruc.CTID,recordId);
		OperatorResult or=Operator.get(BufCompTabCode.Get_By_ID, mUser.getUserID(), filter);
		String tableName = "";
		String tableCname = "";
		
		if(TOOLS.exist(or)){
			tableName = or.getValue(MdStruc.ImpCompTabStruc.EST_TABLE_NAME);
			tableCname = or.getValue(MdStruc.ImpCompTabStruc.EST_TABLE_CNAME);
			if(tableName == null || "".equals(tableName.trim())){
				tableName = or.getValue(MdStruc.ImpCompTabStruc.IMP_TABLE_NAME);
			}
			if(tableCname == null || "".equals(tableCname.trim())){
				tableCname = or.getValue(MdStruc.ImpCompTabStruc.IMP_TABLE_CNAME);
			}
			
			FlatTable ft = new FlatTable(cId,"");
			/**Select名字*/
			ft.putTh(CaptionComp.get(mUser,"254"), 0, 0);
			ft.setColWidth(0, "20",Length_Unit.percent);
			
			ft.putTd(tableName, 0, 1);
			ft.putTd(tableCname, 0,2);
		
			ft.setWidth(width);
			manage.put(ft.get());
		}
	}
	
	private void getTableCol(HtmlWindow manage) throws ErrorMessage{
		DataFilter filter=new DataFilter();
		filter.put(MdStruc.ImpCompTabColStruc.CTID,recordId);
		filter.put(Paging.showPageLine,showPageLine);
		OperatorResult sr=Operator.get(BufCompTabColCode.Get_By_CTID, mUser.getUserID(), filter);
		
		/** 表主键 */
		String mainKey=MdStruc.ImpCompTabColStruc.CCID;
		/** 表格列对应字段名 */
		String[] tableFields={MdStruc.ImpCompTabColStruc.ORID,
							  MdStruc.ImpCompTabColStruc.IMP_COL_NAME,
							  MdStruc.ImpCompTabColStruc.OPERATOR_TYPE,
							  MdStruc.ImpCompTabColStruc.EST_COL_NAME,
							  "operator"};
		/** 表格列类型 */
		TablePropertys.Col_Content_Type[] fieldsType={
				TablePropertys.Col_Content_Type.NAME_TEXT,
				TablePropertys.Col_Content_Type.DESC_TEXT,
				TablePropertys.Col_Content_Type.NAME_TEXT,
				TablePropertys.Col_Content_Type.DESC_TEXT,
				TablePropertys.Col_Content_Type.BUTTON
		};
		/** 设置列标题 */
		String[] tableColTitles={"序号","新导入表列","处理","现有字段","操作"};
		TablePropertys property=new TablePropertys(tableFields,fieldsType,tableColTitles,sHomeUrl,mUser);
		
		/** 设置操作类型*/
		this.setDeal(property);
		/** 设置操作按钮*/
		this.setOperator(property);
		
		/** 创建表对象 */
		FlowTable st=new FlowTable(cId+"table",mainKey,property); 
		/** 将数据集放入表格 */
		st.put(sr);
		st.setWidth(width);
		st.setPageProperty(aId, recordId, cId);
		manage.put(st.get());
	}
	
	/**
	 * 设置处理字段绑定器
	 * @param property
	 * @throws ErrorMessage
	 */
	private void setDeal(TablePropertys property) throws ErrorMessage{
		property.setCallBack(MdStruc.ImpCompTabColStruc.OPERATOR_TYPE, new Buffer2MdDealCallBack());
	}
	
	/**
	 * 设置操作按钮
	 * @param property
	 * @throws ErrorMessage
	 */
	private void setOperator(TablePropertys property) throws ErrorMessage{
		/**设置操作按钮*/
		property.setCallBack("operator", new Buffer2MdColOperatorCallBack());
		/** 设置动作 略过、更新、修改、删除*/
		property.setColAction("operator",Window_Event.OnClick,TablePropertys.Action_Type.SP,
				              Buffer2MdTableColOpt.aId_ignore,null,"ctid,"+recordId);
		property.setColAction("operator",Window_Event.OnClick,TablePropertys.Action_Type.SP,
							  Buffer2MdTableColOpt.aId_update,null,"ctid,"+recordId);
		property.setColAction("operator",Window_Event.OnClick,TablePropertys.Action_Type.SP,
							  Buffer2MdTableColOpt.aId_delete,null,"ctid,"+recordId);
	}
}
